<?php
/**
 * Console 전용 bootstrap
 *
 *
 * @author Platform Team
 */
include dirname(dirname(__FILE__)) . '/bootstrap.php';

/**
 * 콘솔용 파라미터 파싱
 *
 * @param array $noOpt (제외할 옵션 배열)
 */
function parseConsoleParameter($noOpt = array())
{
    $result = array();
    $params = $GLOBALS['argv'];
    // could use getopt() here (since PHP 5.3.0), but it doesn't work relyingly
    reset($params);
    while (list($tmp, $p) = each($params)) {
        if ($p{0} == '-') {
            $pname = substr($p, 1);
            $value = true;
            if ($pname{0} == '-') {
                // long-opt (--<param>)
                $pname = substr($pname, 1);
                if (strpos($p, '=') !== false) {
                    // value specified inline (--<param>=<value>)
                    list($pname, $value) = explode('=', substr($p, 2), 2);
                }
            }
            // check if next parameter is a descriptor or a value
            $nextparm = current($params);
            if (!in_array($pname, $noOpt) && $value === true && $nextparm !== false && $nextparm{0} != '-') list($tmp, $value) = each($params);
            $result[$pname] = $value === true ? '' : $value;
        } else {
            // param doesn't belong to any option
            $result[] = $p;
        }
    }

    return $result;
}

/**
 * 콘솔 자원 초기화
 */
function consoleInitialize()
{
    //Model 초기화
    Model::getInstance();
    Model::instance()->initialize(Configure::get('FW.database'));
}

/**
 * 옵션 파라미터 설정
 */
$GLOBALS['_OPTS'] = parseConsoleParameter();

/**
 * 초기화
 */
consoleInitialize();
